{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "115cb663",
   "metadata": {},
   "source": [
    "## Week05笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d18bc7d3",
   "metadata": {},
   "source": [
    "### 用户输入补充"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "637d4a4e",
   "metadata": {},
   "source": [
    "* 用户输入方法: input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "698f7e28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: name\n",
      "age: age\n",
      "job: job\n",
      "salary: salary\n"
     ]
    }
   ],
   "source": [
    " name = input(\"name: \")\n",
    " age = input(\"age: \")\n",
    " job = input(\"job: \")\n",
    " salary = input(\"salary: \")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "602b0276",
   "metadata": {},
   "source": [
    "* 补充用户信息: 打印 （print）\n",
    "* input -> str  如果想让输入的数据类型变成int 需要强制转变数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c576532",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input(\"name: \")\n",
    "age = int(input(\"age: \"))\n",
    "job = input(\"job: \")\n",
    "salary = int(input(\"salary: \"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe54baa2",
   "metadata": {},
   "source": [
    "* 注释来写内容存到字符串中\n",
    "* 知识点1： 字符串拼接 用 + 让打印的结果更直观\n",
    "* 知识点2： %s占位符，可以把 “+”符号 省略，比较直观的展示变量内容\n",
    "*         缺点： 如果变量很多，就比较麻烦了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "621212f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = '''------INFO %s ------\n",
    "name:%s\n",
    "age:%d\n",
    "job:%s\n",
    "salary:%d '''%(name,name,age,job,salary)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33a05894",
   "metadata": {},
   "source": [
    "* format() 比起%s 更适合较多变量的打印，不需要考量顺序和出现次数\n",
    "* 例如 : url 的拼接 (https:// + 域名 + endpoint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0486f7e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "info1 = '''------INFO {HTML_name} ------\n",
    " name:{HTML_name}\n",
    " age:{HTML_age}\n",
    " job:{HTML_job}\n",
    " salary:{HTML_salary}'''.format(HTML_age=age,HTML_job=job,HTML_name=name,HTML_salary=salary)\n",
    "\n",
    "print(info1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e88619b3",
   "metadata": {},
   "source": [
    "* .format() 是一个字符串的格式化方法/函数 优化 占位符\n",
    "* 优势： 1.不需要考虑参数出现的顺序\n",
    "*       2.多个重复项只需要赋值一次（明确数据输入（如：变量age）和输出（如：打印的HTML_age））"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5ea6df5",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_03 = '''-------INFO {HTML_name} -------\n",
    " name: {HTML_name}\n",
    " age: {HTML_age}\n",
    " job: {HTML_job}\n",
    " salary: {HTML_salary}\n",
    " '''.format(HTML_age=age,HTML_salary=salary,HTML_job=job,HTML_name=name)\n",
    "\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "773737cf",
   "metadata": {},
   "source": [
    "### 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8bbe770f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ytt\n",
      "999\n",
      "23\n",
      "['yt', 666, 28]\n",
      "['P', 'K']\n",
      "['O', 'P']\n",
      "['O', 'P']\n",
      "[]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[1, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "# names = \"zhichao,youge,xiaoming,xiaohong\"\n",
    "# print(names[1])\n",
    "users_list = [\"ytt\",\"yt\",\"y\"]\n",
    "password_list = [999,666,111]\n",
    "age_list = [23,28,18]\n",
    "# ...\n",
    "# 列表的嵌套\n",
    "info_list = [[\"ytt\",999,23],[\"yt\",666,28]]\n",
    "\n",
    "print(users_list[0])\n",
    "print(password_list[0])\n",
    "print(age_list[0])\n",
    "print(info_list[-1])\n",
    "\n",
    "# -3,-2,-1,0,1,2,\n",
    "names = [\"O\",\"P\",\"K\",\"E\"]\n",
    "# 知识点 slices(切片) 数值是指 values在list中的位置\n",
    "\n",
    "# 1. 正向取值\n",
    "# list 取值 “:”右边的值取不到 例如[1:3] 3 这个位置取不到\n",
    "print(names[1:3])\n",
    "print(names[0:2])\n",
    "# 如果 左侧的值是0 即从0开始取值，0可以省略不写\n",
    "print(names[:2])\n",
    "\n",
    "# 2. 反向取值\n",
    "# list 取值 “:”右边的值取不到 例如[-3:-1] -1 这个位置取不到\n",
    "# 注意： 取到最后一个值，不能写 0 省略不写即可\n",
    "print(names[-3:1])\n",
    "\n",
    "num = [1,2,3,4,5,6,7,8,9]\n",
    "# 对比 range\n",
    "print(list(range(10)))\n",
    "print(num[:6:2])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3c1828e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D\n",
      "A\n",
      "['A', 'B', 'C', 'D']\n",
      "['B', 'C']\n",
      "['A', 'B', 'C']\n",
      "['B', 'C']\n",
      "['B', 'C', 'D']\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 3, 6]\n"
     ]
    }
   ],
   "source": [
    "names = [\"A\",\"B\",\"C\",\"D\"]\n",
    "\n",
    "# 取值 \n",
    "# 1. 正数\n",
    "print(names[3])\n",
    "# 2. 负数\n",
    "print(names[-4])\n",
    "\n",
    "# 切片\n",
    "# print(names[5])\n",
    "# IndexError: list index out of range\n",
    "print(names[:])\n",
    "# 1. 在从左往右取值时，右边的值取不到\n",
    "print(names[1:3])\n",
    "# 如果从0开始取值，0 可省略不写\n",
    "print(names[:3])\n",
    "\n",
    "# 2. 在从右往左取值时,右边的值取不到\n",
    "print(names[-3:-1])\n",
    "\n",
    "# 怎么能取到-1这个位置的值呢？ 右侧不写任何值\n",
    "print(names[-3:])\n",
    "#\n",
    "# 3. 特殊情况 可以指定 slice 切片的步长 step\n",
    "num_list = list(range(10))\n",
    "print(num_list)\n",
    "print(num_list[0:7:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "98a1a61b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C', 1, 20]\n",
      "[['A', 0, 19], ['B', 10, 61], ['C', 1, 20]]\n",
      "[['A', 0, 19], ['B', 10, 61]]\n",
      "[['A', 0, 19], ['B', 10, 61]]\n",
      "[['A', 0, 19], ['B', 10, 61]]\n",
      "[]\n",
      "[['A', 0, 19], ['B', 10, 61], ['C', 1, 20]]\n",
      "[['A', 0, 19], ['B', 10, 61], ['C', 1, 20]]\n",
      "[['C', 1, 20]]\n",
      "[]\n",
      "[0, 1, 2, 3, 4, 5]\n",
      "[0, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "user_data_list = [[\"A\",0,19],[\"B\",10,61],[\"C\",1,20]]\n",
    "# password_list = [0,00,1]\n",
    "# age = [19,61,20]\n",
    "\n",
    "\n",
    "#1.从左往右取值 （index&slice）\n",
    "print(user_data_list[2])\n",
    "\n",
    "#slice切片过程中,右边值取不到 比如0：3只能取到0，1，2\n",
    "print(user_data_list[0:3])\n",
    "\n",
    "#当从0开始取值时，0可以忽略不写\n",
    "print(user_data_list[:2])\n",
    "\n",
    "#从左往右取值 （index&slice）\n",
    "print(user_data_list[-3:-1])\n",
    "\n",
    "#右边的数值永远取不到，例如上面的-1取不到\n",
    "print(user_data_list[-3:-1])\n",
    "\n",
    "#如果取指为空，说明slice写错了 例如[-3:0]\n",
    "#如果要取最后一个值，不能写0，直接省略不写\n",
    "print(user_data_list[-3:0])  # 错误\n",
    "print(user_data_list[-3:])  # 正确！\n",
    "\n",
    "#列出列表全部内容\n",
    "print(user_data_list[:])\n",
    "#指全体数值\n",
    "print(user_data_list[2:])\n",
    "print(user_data_list[3:1])  # 错误\n",
    "\n",
    "\n",
    "# 三个重要的值\n",
    "#  步长，list列表slice ：[start:stop:step]\n",
    "# [start:stop:step] 分别对应开始值，结束值，步长（间隔）。\n",
    "# 开始值默认为0，结束值默认为方框内规定的数字，步长默认为1（下面实例中改为2）\n",
    "\n",
    "# 从0到6，但6取不到，默认从0开始，0取得到。\n",
    "num = list(range(6)) \n",
    "# 输出上面的列表\n",
    "print(num)           \n",
    "# 0为开始值，6为结束值，2为步长。\n",
    "print(num[0:6:2])   \n",
    "\n",
    "#   重要！\n",
    "# 如果没有指定开始值，则默认为0；\n",
    "# 如果没有指定结束指，则取列表允许的最大值；\n",
    "# 如果没有指定步长值，则默认步长为1."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
